package cn.yudao.basic.controller;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.service.IVerifyCodeService;
import cn.yudao.basic.service.impl.VerifyCodeServiceImpl;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.vo.RegisterVo;
import cn.yudao.org.domain.Shop;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
    * @Title: getImageVerifyBase
    * @Description: 返回图片验证码的base64编码
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/5 17:26
    * @Parameters:
    * @Return
    */
    @GetMapping("/image/{item}")
    public AjaxResult getImageVerifyBase(@PathVariable("item") String item){
        try {
            if( item==null && "".equals(item)){
                return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
            }
            String imgBase = verifyCodeService.getImgBase(item);
            return AjaxResult.me().setResult(imgBase);
        }catch (BusinessException e){
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: smsCode
    * @Description: 获取手机验证码
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 10:14
    * @Parameters:
    * @Return
    */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterVo registerVo){
        try {
            verifyCodeService.smsCode(registerVo);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }


    /**
    * @Title: emailCode
    * @Description: 发送邮件以及验证码
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 10:14
    * @Parameters:
    * @Return
    */
    @PostMapping("/emailCode")
    public AjaxResult emailCode(@RequestBody RegisterVo registerVo){
        try {
            verifyCodeService.emailCode(registerVo);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

//    // PhoneCode
//    @PostMapping("/login/phoneCode")
//    public AjaxResult loginPhoneCode(@RequestBody RegisterVo registerVo){
//        try {
//            verifyCodeService.loginPhoneCode(registerVo);
//            return AjaxResult.me();
//        }catch (BusinessException e){
//            e.printStackTrace();
//            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
//        }catch (Exception e) {
//            e.printStackTrace();
//            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
//        }
//    }

}
